//
//  SEExamineRecordVisionController.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/15.
//

import UIKit

class SEExamineRecordVisionController: SEExamineRecordChildBaseController {

    var viewModel = SEExamineRecordVisionViewModel()
    
    override func setupUI() {
        super.setupUI()
        
        tableView.dataSource = self
        
        tableView.register(SEExamineRecordVisionCell.nibForCell(),
                           forCellReuseIdentifier: SEExamineRecordVisionCell.reuseIdentifier())
        tableView.register(SEExamineRecordImageCell.nibForCell(),
                           forCellReuseIdentifier: SEExamineRecordImageCell.reuseIdentifier())

        cancelReserveView.numberLabel.text = viewModel.model.internalCheckNum
    }
}

// MARK: - UITableViewDataSource

extension SEExamineRecordVisionController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel.numberOfSections
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.numberOfRowInSection(section: section)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == viewModel.numberOfSections - 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: SEExamineRecordImageCell.reuseIdentifier()) as! SEExamineRecordImageCell
            cell.updateWithViewModel(viewModel.examineImgCellModel(index: indexPath.row))
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: SEExamineRecordVisionCell.reuseIdentifier()) as! SEExamineRecordVisionCell
            cell.updateWithViewModel(viewModel.examineRecordData(index: indexPath.section))
            return cell
        }
    }
}

// MARK: - UITableViewDelegate

extension SEExamineRecordVisionController {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return viewModel.heightForRow(indexPath: indexPath)
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = UINib.loadViewFrom(SEExamineRecordSectionHeader.self)
        header.updateWithViewModel(nil)
        header.text = viewModel.titleForHeaderInSection(section: section)
        return header
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return viewModel.heightForHeader(section: section)
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.001
    }
}
